import { ObjectId } from 'mongodb';
import BaseModel from './MongoBaseModel';

interface IPerson {
  _id?: ObjectId;
  id?: number;
  name?: string;
  [propName: string]: any;
}

class Model extends BaseModel<IPerson> {
  constructor() {
    super('persons');
  }

  async getListJoin(query) {
    const db = await this.init();
    const res = db
      .aggregate([
        {
          $match: query,
        },
        {
          $lookup: {
            from: 'role',
            localField: 'role',
            foreignField: '_id',
            as: 'roleInfo',
          },
        },
        {
          $project: {
            _id: 1,
            name: 1,
            group: 1,
            groupLeader: 1,
            weight: 1,
            isEdit: 1,
            onDuty: 1,
            role: '$roleInfo',
          },
        },
        {
          $unwind: { path: '$role', preserveNullAndEmptyArrays: true },
        },
      ])
      .toArray();
    const ret = db.find(query || {});
    return res;
  }
}

export default new Model();
